package com.bujunjie.study.realtime.dws.app.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>关键字分割工具类</p>
 *
 * @author bu.junjie
 * @version 1.0.0
 * @createTime 2025/9/19 15:16
 */
public class KeywordUtil {

    /**
     * <p>将词进行分割</p>
     *
     * @param text 将词进行分割
     * @return 返回分割的文本集合
     * @author bu.junjie
     * @date 2025/9/19 15:17
     */
    public static List<String> analyze(String text) {
        List<String> keywordList = new ArrayList<>();
        StringReader reader = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(reader, true);
        try {
            Lexeme lexeme = null;
            while ((lexeme = ik.next()) != null) {
                String keyword = lexeme.getLexemeText();
                keywordList.add(keyword);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return keywordList;
    }
}
